/*    */ package com.wgcloud.util.license;
/*    */ 
/*    */ import cn.hutool.core.codec.Base64;
/*    */ import java.security.InvalidKeyException;
/*    */ import java.security.KeyFactory;
/*    */ import java.security.NoSuchAlgorithmException;
/*    */ import java.security.interfaces.RSAPublicKey;
/*    */ import java.security.spec.InvalidKeySpecException;
/*    */ import java.security.spec.X509EncodedKeySpec;
/*    */ import javax.crypto.BadPaddingException;
/*    */ import javax.crypto.Cipher;
/*    */ import javax.crypto.IllegalBlockSizeException;
/*    */ import javax.crypto.NoSuchPaddingException;
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ public class RSAEncrypt
/*    */ {
/*    */   public static RSAPublicKey loadPublicKeyByStr(String publicKeyStr) throws Exception {
/*    */     try {
/* 30 */       byte[] buffer = Base64.decode(publicKeyStr);
/* 31 */       KeyFactory keyFactory = KeyFactory.getInstance("RSA");
/* 32 */       X509EncodedKeySpec keySpec = new X509EncodedKeySpec(buffer);
/* 33 */       return (RSAPublicKey)keyFactory.generatePublic(keySpec);
/* 34 */     } catch (NoSuchAlgorithmException e) {
/* 35 */       throw new Exception("无此算法");
/* 36 */     } catch (InvalidKeySpecException e) {
/* 37 */       throw new Exception("公钥非法");
/* 38 */     } catch (NullPointerException e) {
/* 39 */       throw new Exception("公钥数据为空");
/*    */     } 
/*    */   }
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */   
/*    */   public static byte[] decrypt(RSAPublicKey publicKey, byte[] cipherData) throws Exception {
/* 53 */     if (publicKey == null) {
/* 54 */       throw new Exception("解密公钥为空, 请设置");
/*    */     }
/* 56 */     Cipher cipher = null;
/*    */     
/*    */     try {
/* 59 */       cipher = Cipher.getInstance("RSA");
/*    */       
/* 61 */       cipher.init(2, publicKey);
/* 62 */       byte[] output = cipher.doFinal(cipherData);
/* 63 */       return output;
/* 64 */     } catch (NoSuchAlgorithmException e) {
/* 65 */       throw new Exception("无此解密算法");
/* 66 */     } catch (NoSuchPaddingException e) {
/* 67 */       e.printStackTrace();
/* 68 */       return null;
/* 69 */     } catch (InvalidKeyException e) {
/* 70 */       throw new Exception("解密公钥非法,请检查");
/* 71 */     } catch (IllegalBlockSizeException e) {
/* 72 */       throw new Exception("密文长度非法");
/* 73 */     } catch (BadPaddingException e) {
/* 74 */       throw new Exception("密文数据已损坏");
/*    */     } 
/*    */   }
/*    */ }


/* Location:              /Users/zhangyongxin/soft-install/wgcloud/wgcloud-v3.3.5/server/bank/wgcloud-server-release.jar!/BOOT-INF/classes/com/wgcloud/util/license/RSAEncrypt.class
 * Java compiler version: 8 (52.0)
 * JD-Core Version:       1.1.3
 */